package com.example.administrator.jingpingou.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.jingpingou.R;
import com.example.administrator.jingpingou.activity.RegisterActivity;
import com.example.administrator.jingpingou.activity.MineCollectActivity;
import com.example.administrator.jingpingou.application.MyApplication;
import com.example.administrator.jingpingou.bean.LoginUserBean;
import com.example.administrator.jingpingou.login.LoginActivity;
import com.example.administrator.jingpingou.ui.MineTaoActivity;
import com.example.administrator.jingpingou.ui.MineTaoCatActivity;
import com.example.administrator.jingpingou.ui.SheZhiActivity;

import org.xutils.DbManager;
import org.xutils.db.Selector;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.List;

/**
 * Created by Administrator on 2016/8/28 0028.
 */
public class MineFragment extends Fragment {

    private boolean flag;
    private ImageView mImageIcon;
    private TextView mTextName;
    private ImageView mImageViewMine,mImageViewWuLiu,mImageViewGouWuChe,mImageViewDingDan,mImageViewShouCang,mImageViewShezhi;
    private DbManager mDb;
    private String name;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_mine, null);
        iniView(view);
        setListener();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
        if (TextUtils.isEmpty(MyApplication.getUserName())){
            mTextName.setText("游客登录");
        }else {
            mTextName.setText(MyApplication.getUserName());
//            mImageIcon.setVisibility(View.GONE);
        }
    }

    private void initData() {
        DbManager.DaoConfig config =new DbManager.DaoConfig();
        config.setDbName("UserLogin");
        mDb = x.getDb(config);
//        if (mDb!=null){
//
//            try {
//                List<LoginUserBean> userBeen =
//                        mDb.selector(LoginUserBean.class).findAll();
//                if (userBeen!=null){
//                    for (int i = 0; i < userBeen.size(); i++) {
//                        LoginUserBean loginUserBean = userBeen.get(i);
//                        name = loginUserBean.getName();
//                    }
//                }else {
//                    startActivity(new Intent(getActivity(),RegisterActivity.class));
//                }
//            } catch (DbException e) {
//                e.printStackTrace();
//            }


//        }

    }

    private void setListener() {
        mImageViewShezhi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(),SheZhiActivity.class));
            }
        });
        mImageViewGouWuChe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(),MineTaoCatActivity.class));
            }
        });
        mImageViewWuLiu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(),MineTaoActivity.class));
            }
        });
        mImageViewMine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(MyApplication.getUserName())) {
                    if (flag) {
                        startActivity(new Intent(getActivity(), RegisterActivity.class));
                    } else {
                        startActivity(new Intent(getActivity(), LoginActivity.class));
                    }
//                startActivity(new Intent(getActivity(),MineTaoActivity.class));
                }else {
                    // TODO: 2016/9/1 弹出窗口注销登录  点击时把MyApplication中的username改成空

                }
            }
        });

        mImageViewDingDan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(),MineTaoActivity.class));
            }
        });
        mImageViewShouCang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(MyApplication.getUserName())){
                    Toast.makeText(getActivity(), "请先登录", Toast.LENGTH_SHORT).show();
                }else {
                startActivity(new Intent(getActivity(),MineCollectActivity.class));}
            }
        });
    }

    private void iniView(View view) {
        mImageViewMine= (ImageView) view.findViewById(R.id.iv_mine_wode);
        mImageViewWuLiu= (ImageView) view.findViewById(R.id.iv_mine_wuliu);
        mImageViewGouWuChe= (ImageView) view.findViewById(R.id.iv_mine_gouwuche);
        mImageViewDingDan= (ImageView) view.findViewById(R.id.iv_mine_dingdan);
        mImageViewShouCang= (ImageView) view.findViewById(R.id.iv_mine_shoucang);
        mImageViewShezhi= (ImageView) view.findViewById(R.id.iv_mine_sezhi);
//        mImageIcon = (ImageView) view.findViewById(R.id.iv_fragment_mine_icon);
        mTextName = (TextView) view.findViewById(R.id.tv_fragment_mine_name);
    }
}
